<?php

/**
 * @author gbfun <gbfun@163.com>  2017-09-16 20:03
 * http://www.wuweiyou.com
 */

class db_model_good
{
    protected $_db_table;   
    protected $_formatter;
    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    private function __clone()
    {
    }
    
    protected function __construct()
    {
    }

    public function get_db_table()
    {
        if (null === $this->_db_table) {
            $this->_db_table = db_table_good::get_instance();
        }
        
        return $this->_db_table;
    }
    
    public function get_formatter()
    {
        if (null === $this->_formatter) {
            $this->_formatter = formatter_good::get_instance();
        }
    
        return $this->_formatter;
    }
    
    public function set_rows(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row($row);            
        }
    }
    
    public function set_row(& $row)
    {
        $formatter = $this->get_formatter();
        
        $formatter->set_by_name($row);
        $formatter->set_by_image($row);
        
        $formatter->set_by_create_time($row);
        $formatter->set_by_update_time($row);
        $formatter->set_by_status($row);
    }
    
    public function set_rows_user(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row_user($row);
        }
    }
    
    public function set_row_user(& $row)
    {
        $formatter = $this->get_formatter();
        
        $formatter->set_by_image_arr($row);
    }
    
    public function find($id, $status = null)
    {
        $row = $this->get_db_table()->find($id, $status);

        if(!empty($row)){
            $this->set_row($row);
        }
    
        return $row;
    }

    public function fetch_all($where = null, $order = null)
    {
        $rows = $this->get_db_table()->fetch_all($where, $order);
        
        $this->set_rows($rows);
        
        return $rows;        
    }
    
    public function get_list($where = null, $order = null, $options = array())
    {
        $list = $this->get_db_table()->get_list($where, $order, $options);

        $this->set_rows($list['list']);
    
        return $list;
    }
    
    public function get_simple_list($where = null, $order = null, $options = array())
    {
    
    }
    
    public function get_attr_list($where = null, $order = null)
    {
        $attr_list = $this->get_db_table()->get_attr_list($where, $order);
        //var_dump($attr_list); exit();
    
        return $attr_list;
    }
 
    public function get_sattr_list($where = null, $order = null)
    {
        $sattr_list = $this->get_db_table()->get_sattr_list($where, $order);
        //var_dump($sattr_list); exit();
    
        return $sattr_list;
    }
    
    public function get_number_grade_list($where = null, $order = null)
    {
        $number_grade_list = $this->get_db_table()->get_number_grade_list($where, $order);
        //var_dump($number_grade_list); exit();
    
        return $number_grade_list;
    }
    
    public function get_spec_list($where = null, $order = null)
    {
        $spec_list = $this->get_db_table()->get_spec_list($where, $order);
        //var_dump($spec_list); exit();
        
        foreach($spec_list as $key => $spec){
            $where = array('good_spec_id' => $spec['id']);
            //var_dump($where); exit();
            
            $spec_sattr_value_list = $this->get_spec_sattr_value_list($where);
            //var_dump($spec_sattr_value_list); exit();
            $spec['sattr_value_list'] = $spec_sattr_value_list;
            //var_dump($spec); exit();
            
            $spec_number_grade_price_list = $this->get_spec_number_grade_price_list($where);
            //var_dump($spec_number_grade_price_list); exit();
            $spec['number_grade_price_list'] = $spec_number_grade_price_list;
            //var_dump($spec); exit();
            
            $spec_list[$key] = $spec;
            //var_dump($spec_list); exit();
        }
        //var_dump($spec_list); exit();
    
        return $spec_list;
    }
    
    public function get_spec_item_simple($good_id, $spec_id)
    {
        $spec_item = $this->get_db_table()->get_spec_item($good_id, $spec_id);
        
        return $spec_item;
    }
    
    public function get_spec_item($good_id, $spec_id)
    {
        //规格
        $spec_item = $this->get_db_table()->get_spec_item($good_id, $spec_id);
        //var_dump($spec_item); exit();
        
        if(!empty($spec_item)){        
            //获取规格动态属性
            $where = array('good_id' => $good_id);
            $order = array('`order` asc');
            $sattr_list = $this->get_sattr_list($where, $order);
            //var_dump($sattr_list); exit();
                        
            //规格_属性_值
            $where = array('good_spec_id' => $spec_item['id']);
            $spec_sattr_value_list = $this->get_spec_sattr_value_list($where);
            //var_dump($spec_sattr_value_list); exit();
        
            //根据属性【排序】和【id】， 获取【规格_属性_值】的【文字描述】
            $spec_sattr_value_descs = array();
            foreach($sattr_list as $sattr){                
                $sattr_id   = $sattr['id'];
                $sattr_name = $sattr['name'];
                
                $spec_sattr_value = $spec_sattr_value_list[$sattr_id]['value'];
                $spec_sattr_value_desc = $sattr_name . '：' . $spec_sattr_value;
                $spec_sattr_value_descs[] = $spec_sattr_value_desc;
            }
            $spec_item['spec_desc'] = implode(' ', $spec_sattr_value_descs);    
        }
            
        return $spec_item;
    }
    
    
    
    public function get_match_spec_number_grade_price($good_id, $spec_id, $good_number)
    { 
        $match_spec_number_grade_price = array();
        //return $match_spec_number_grade_price;
        
        $match_number_grade = $this->get_match_number_grade($good_id, $good_number);
        if(empty($match_number_grade)){
            return $match_spec_number_grade_price;
        }
        
        //规格_数量范围_价格
        $where = array(
            'good_spec_id' => $spec_id,
            'good_number_grade_id' => $match_number_grade['id'],            
        );
        $match_spec_number_grade_price = $this->get_db_table()->get_match_spec_number_grade_price($where);
        //var_dump($match_spec_number_grade_price); exit();
    
        return $match_spec_number_grade_price;
    }
    
    public function get_match_number_grade($good_id, $good_number)
    {
        //获取数量范围
        $where = array('good_id' => $good_id);
        $order = array('`order` asc');
        $number_grade_list = $this->get_number_grade_list($where, $order);
        //var_dump($number_grade_list); exit();
        
        $match_number_grade = array();
        foreach($number_grade_list as $number_grade){
            if( ($number_grade['number_min'] == 0) && ($number_grade['number_max'] == 0) ){
                /* 其实可以【不用】这里的判断，【else】里【已包含】这里的判断了 */
                
                $match_number_grade = $number_grade;
                break;
            }else{
                if($number_grade['number_min'] > 0){
                    if($good_number < $number_grade['number_min']){
                        continue;
                    }
                }
                
                if($number_grade['number_max'] > 0){
                    if($good_number > $number_grade['number_max']){
                        continue;
                    }
                }
                
                $match_number_grade = $number_grade;
                break;
            }            
        }
        //var_dump($match_number_grade); exit();
               
        return $match_number_grade;        
    }
    
    
    public function get_spec_sattr_value_list($where = null)
    {
        $spec_sattr_value_list_db = $this->get_db_table()->get_spec_sattr_value_list($where);
        //var_dump($spec_sattr_value_list_db); exit();
                
        $spec_sattr_value_list = array();
        foreach($spec_sattr_value_list_db as $spec_sattr_value){
            $good_sattr_id = $spec_sattr_value['good_sattr_id'];
            
            $spec_sattr_value_list[$good_sattr_id] = $spec_sattr_value;
        }
    
        return $spec_sattr_value_list;
    }
    
    public function get_spec_number_grade_price_list($where = null)
    {
        $spec_number_grade_price_list_db = $this->get_db_table()->get_spec_number_grade_price_list($where);
        //var_dump($spec_number_grade_price_list_db); exit();
    
        $spec_number_grade_price_list = array();
        foreach($spec_number_grade_price_list_db as $spec_number_grade_price){
            $good_number_grade_id = $spec_number_grade_price['good_number_grade_id'];
    
            $spec_number_grade_price_list[$good_number_grade_id] = $spec_number_grade_price;
        }
    
        return $spec_number_grade_price_list;
    }
    
    public function increase_click($id)
    {        
        $increase_click_result = $this->get_db_table()->increase_click($id);
        //var_dump($increase_click_result); exit();
        
        return $increase_click_result;
    }
}

